/**
 * Created by a1 on 2017/5/25.
 */
_$(function(){
    var init,
        loadAjax,
        bindEvent,
        doDom,
        recode=false;
    init=function(){
        loadAjax();
        bindEvent();
        doDom();
    };
    loadAjax=function(){

    };
    bindEvent=function(){

    };
    doDom=function(){
        _$("#pic").click(function(){
            _$(this).attr('src','/recode?num='+Math.random());
        });
        _$("#pswClose,#psw1Close,#userClose,#codeClose,#emailClose").click(function(){
            _$(this).siblings('input').val("");
        });
        var showPsw=_$("#showPsw").is(':checked');
        if(showPsw==true){
            _$('#password,#password1').attr('type','password');
        }
        _$('#showPsw').change(function(){
            if(_$(this).is(':checked')){
                _$('#password,#password1').attr('type','password');
            }else{
                _$('#password,#password1').attr('type','text');
            }
        });
        _$("#rescode").keyup(function(){
            _$.post('/dorecode',{code:_$(this).val().toUpperCase()},function(json){
                if(json['code']==1){
                    recode=true;
                }else{
                    recode=false;
                }
            })
        });
        function isShowBtn(oneD,twoD,threeD,fourD,faieD){
            _$(oneD).keyup(function(){
                var one=_$(this).val();
                var two=_$(twoD).val();
                var three=_$(threeD).val();
                var four=_$(fourD).val();
                var faie=_$(faieD).val();
                if(one!=""&&two!=""&&three!=""&&four!=""&&faie!=""){
                    _$("#regBtn").removeClass('disabled').removeAttr('disabled');
                }else{
                    _$("#regBtn").addClass('disabled').attr('disabled',true);
                }
            })
        }
        isShowBtn('#password','#password1','#username','#email','#rescode');
        isShowBtn('#password1','#password','#username','#email','#rescode');
        isShowBtn('#username','#password1','#password','#email','#rescode');
        isShowBtn('#email','#password1','#password','#username','#rescode');
        isShowBtn('#rescode','#password1','#password','#username','#email');
        _$("#regBtn").click(function(){
            if(_$(this).is('disabled')==true){

            }else{
                var usm=_$("#username").val();
                var psw=_$("#password").val();
                var email=_$("#email").val();
                var psw1=_$("#password1").val();
                if(recode==false){
                    _$(".msg").html('验证码出错');
                }else if(isUsm(usm)==false){
                    _$(".msg").html('请将用户名位数设置为2-20位');
                }else if(isEmail(email)==false){
                    _$(".msg").html('邮箱格式出错');
                }else if(isPsw(psw)==false){
                    _$(".msg").html('请将密码位数设置为6-20位');
                }else if(psw!=psw1){
                    _$(".msg").html('确认密码不对');
                }else{
                    _$(".msg").html('');
                    _$.post('/register',{
                        username:usm,
                        email:email,
                        password:psw
                    },function(json){
                        console.log(json['code']);
                        if(json['code']==1){
                            _$(".msg").html(json['retMsg']);
                            _$("#regBtn").attr('disabled',true);
                            setTimeout(function(){
                                location.href='/login';
                            },1000);
                        }else if(json['code']==0){
                            _$(".msg").html(json['retMsg']);
                        }else if(json['code']==2){
                            _$(".msg").html(json['retMsg']);
                        }else{
                            _$(".msg").html(json['retMsg']);
                        }
                    });
                }
            }
        });
        function isEmail(str){
            var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
            return reg.test(str);
        }
        function isPsw(str){
            var reg = /^[A-Za-z0-9]{6,20}$/;
            return reg.test(str);
        }
        function isUsm(str){
            var reg = /^[A-Za-z0-9]{2,20}$/;
            return reg.test(str);
        }
    };
    init();
});